import Row from "../../qbasics/basics/Row";
/**
 * 分词
 * 词法分析
 */

export default class Lex{
    constructor(core){
        this.core=core;
    }


    /**
     *分词
     */
    run(wordIds){
        //字 数
        //是否是组合
        // 比如，结构，名词，动词
        let _this=this;
        let signs=[];
        let field='1' ; //是不是结构词
        let act='0' ; //
        let speech='1'; //名词
        let noun='0'; //数
        let symbol='1';//是否是 结构
        let id=0;
        for(let i=0;i<wordIds.length;i++,id++){

            let wordIdA=wordIds[i];
            let wordIdB=wordIds[i+1];

            let wordSign= _this.findSign(wordIdA)

            //两个字一样
            if(wordIds[i]==wordIds[i+1]){
                let wordIdz=wordIds[i];
                let list=[];
                while (wordIdA==wordIdz) {
                    list.push(wordIdA)
                    i++;
                    wordIdA=wordIds[i];
                }
                i--;
                signs.push({id:id,type:1,remarks:"词ID:重复词",wordIdList:list,wordIdKey:wordIdA })
                continue
            }


            let wordsId=_this.findWrodsId(wordIdA,wordIdB)
            if(wordsId){
                let words= _this.findSign(wordsId)
                if(_this.isSign(words,field,act,speech)){

                    let wordIdC=wordIds[i+2];
                    let isWords3=_this.findWrodsId(wordIdB,wordIdC)
                    let isWords3B=false;
                    if (isWords3){
                        let wordSign4= _this.findSign(wordIdC)
                        if(_this.isSign(wordSign4,field,act,speech)||_this.isSign(wordSign4,field,act,noun))   isWords3B=true
                    }
                    let wordSign4= _this.findSign(wordIdC)

                    if(isWords3B||_this.isSign(wordSign4,field,act,noun)){
                        let list=[wordIdA,wordIdB,wordIdC];
                        signs.push({id:id,type:4,remarks:"词ID:words3",wordIdList:list,wordIdKey:wordIdC,wordsId:wordIdC })
                        i=i+2;
                        continue
                    }
                    let list=[wordIdA,wordIdB];
                    signs.push({id:id,type:3,remarks:"词ID:words2",wordIdList:list,wordsId:wordsId})
                    i++;
                    continue
                }

            }

            /**
             * 如果字符有特殊标记
             */

            if(_this.isSign(wordSign,field,act,symbol)){
                //遇到特殊标记开始检查栈数据是否完整。
                //检查清空栈

                switch(wordSign) {
                    //数字
                    case '1':

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                            wordId=wordIds[i];
                            wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break;
                    case '1': //标点符号

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                            wordId=wordIds[i];
                            wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break;
                    case '1'://特殊字符

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                            wordId=wordIds[i];
                            wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break
                    default:
                        let  list=[wordIdA];
                        signs.push({id:id,type:7,remarks:'标记',wordIdList:list,wordIdKey:wordIdA})
                        break
                }
                continue
            }
            let wordBSign= _this.findSign(wordIdB)
            /**
             * 检查 wordIdB 是不是有没有特殊标记B
             */
            if(_this.isSign(wordBSign,field,act)){
                /* let isStack=this.checkStack(stack);
                 if(isStack){
                     heap.push(stack);
                     stack=[];
                 }*/
                let list=[wordIdA];
                signs.push({id:id,type:6,remarks:'剩字',wordIdList:list,wordIdKey:wordIdA})
                list=[wordIdB];
                signs.push({id:id,type:7,remarks:'标记',wordIdList:list,wordIdKey:wordIdA})
                i++;
                continue
            }


            let wordIdC=wordIds[i+2];
            let wordSign4= _this.findSign(wordIdC)

            if(_this.isSign(wordSign4,field,act,noun)){
                let list=[wordIdA,wordIdB,wordIdC];
                signs.push({id:id,type:2,remarks:"生词:" +'words3',wordIdList:list,wordIdKey:wordIdC})
                i=i+2;
                continue
            }
            let list=[wordIdA,wordIdB];
            signs.push({id:id,type:1,remarks:"生词:" +'words2',wordIdList:list})
            i++;
            continue
        }
        return signs


    }


    /**
     * 检查是否标记
     * @param wordSign
     */
    isSign(wordSign,field,act,idea){
        /**
         *  数字  digit
         *  符号 other
         *  单位 unit
         *
         * @type {Array}
         */
        if(wordSign){
            for(let l=0 ;l<wordSign.list.length;l++) {
                let row =wordSign.list[l];
                let res=[];
                if(row.isValue(1,field)) res.push(1);
                if(row.isValue(4,idea)) res.push(1)
                if (res.length==2) {
                     return true
                }
            }
        }


        return false;
    }

    /**
     * 查找字符 的标记
     * @param wordId
     * @returns {null}
     */
    findSign(wordId){
        return  this.core.findReal(null,wordId)
    }


    //查询是不是的词
    findWrodsId(wordIdA,wordIdB){
        let words= new Row( wordIdA,wordIdB,null,2)
        let rowId= this.findEmptyId(words,null,'no');
        return rowId
    }


    /**
     *
     * @param row
     * @param func
     * @param resType  Default empty  id
     * @returns {*}
     */
    findEmptyId(row,func,resType){
        if(resType){
            this.resEmptyType=resType;
        }else {
            this.resEmptyType='empty';
        }
        if(row){
            this.emptyRow=row;
        }else {
            this.emptyRow=new Row(null,null,null,null);
        }
        if(func){
            return this.core.wordr.findRoundData(0,null,null,func)
        }
        return this.core.wordr.findRoundData(0,null,null,this.empthDefaultFunc.bind(this))
    }
    empthDefaultFunc(roundId,BlockId,RowId,dataThis){
        let word=this.emptyRow;
        for(let n=0;n<dataThis.list.length;n++){
            let row=  dataThis.list[n];
            let res=[];
            if(word.find(1)===null||row.isValue(1,word.find(1)) ) {
                res.push(1)
            }
            if(word.find(2)===null||row.isValue(2,word.find(2))  ){
                res.push(1)
            }
            if(word.find(3)===null||row.isValue(3,word.find(3))  ){
                res.push(1)
            }
            if(word.find(4)===null||row.isValue(4,word.find(4))  ){
                res.push(1)
            }
            if(res.length==4){
                if(this.resEmptyType=='empty') {
                    return row;
                }
                return n;
            }
        }
        return null;

    }
}